const http = require("../../../utils/request");
//index.js
Component({
  data: {
    TimeVisible: false,
    TimeValue:"7",
    TimeText:"7天之内",
    Times:[
      {label:"7天之内",value:"7"},
      {label:"近一个月之内",value:"30"},
      {label:"近半年内",value:"180"},
    ],
    total: 0,
    loss: 0,
    lossRate: 0 ,
    chartData_cul: {},
    chartData_trans: {},
    transData:[],
    transVal:[],
    opts_cul: {
        color: ["#1890FF","#91CB74"],
        padding: [2,2,2,2],
        enableScroll: false,
        extra: {
          pie: {
            activeOpacity: 0.2,
            activeRadius: 6,
            offsetAngle: 0,
            labelWidth: 15,
            border: true,
            borderWidth: 3,
            borderColor: "#FFFFFF",
            linearType: "custom"
          },
        }
      },
    opts_trans:{
      color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
      padding: [15,30,20,5],
      touchMoveLimit: 24,
      enableScroll: true,
      legend: {},
      xAxis: {
        disableGrid: true,
        scrollShow: true,
        itemCount: 8,
        rotateLabel: true,

      },
      yAxis: {
        data: [
          {
            min: 0
          }
        ]
      },
      extra: {
        column: {
          type: "group",
          width: 20,
          activeBgColor: "#000000",
          activeBgOpacity: 0.08
        }
      }
  }
},
  lifetimes:{
    ready(){
      http.getRequest("/admin/console/cultivation?count="+this.data.TimeValue,{},res=>{
        if(res.data.data){
          this.setData({
            total: res.data.data.totalCount,
            loss: res.data.data.lossBreedingCount,
            lossRate: res.data.data.lossRate,
          })
          this.getServerData_cul(res.data.data.breedingRate,res.data.data.fattenedRate);
        }else{
          this.setData({
            total: 0,
            loss: 0,
            lossRate: 0,
          })
          this.getServerData_cul(0,0);
        }
      })
      http.getRequest("/admin/console/destination?count="+100,{},res=>{
        console.log('trans',res.data.data);
        let transData = [];
        let transVal = [];
        for(let v of res.data.data){
          transData.push(v.destination);
          transVal.push(v.totalCount);
        }
        this.setData({
          transData,
          transVal
        })
      })
      this.getServerData_trans();
    }
  },
  methods: {
    getServerData_cul( ins , outs) {
      //模拟从服务器获取数据时的延时
      setTimeout(() => {
        //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
        let res = {
          series: [
            {
              data: [{"name":"已出栏","value":ins},{"name":"养殖中","value":outs},]
            }
          ]
        };
      this.setData({ chartData_cul: JSON.parse(JSON.stringify(res)) });
    }, 500);
    },
    getServerData_trans() {
      //模拟从服务器获取数据时的延时
      setTimeout(() => {
        //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
        let res = {
          categories: this.data.transData,
          series: [
            {
              name: "运输值",
              data: this.data.transVal
            },
          ]
        };
      this.setData({chartData_trans : JSON.parse(JSON.stringify(res))});
    }, 500);
    },
    onColumnChange(e) {
      console.log('picker pick:', e);
    },

    onPickerChange(e) {
      const { key } = e.currentTarget.dataset;
      const { value } = e.detail;

      console.log('picker change:', e.detail);
      this.setData({
        [`${key}Visible`]: false,
        [`${key}Value`]: value,
        [`${key}Text`]: e.detail.label.join(' '),
      });
      http.getRequest("/admin/console/cultivation?count="+this.data.TimeValue,{},res=>{
        if(res.data.data){
          this.setData({
            total: res.data.data.totalCount,
            loss: res.data.data.lossBreedingCount,
            lossRate: res.data.data.lossRate,
          })
          this.getServerData_cul(res.data.data.breedingRate,res.data.data.fattenedRate);
        }else{
          this.setData({
            total: 0,
            loss: 0,
            lossRate: 0,
          })
          this.getServerData_cul(0,0);
        }
      })
    },

    onPickerCancel(e) {
      const { key } = e.currentTarget.dataset;
      console.log(e, '取消');
      console.log('picker1 cancel:');
      this.setData({
        [`${key}Visible`]: false,
      });
    },
    onTimePicker() {
      this.setData({ TimeVisible: true });
    },

  }
})

